<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
class VoucherTypeTable extends AbstractTable
{
    protected $table = 'hy_voucher_type';
    protected $id = 'vt_id';
    
    
    public function getVoucherTypeList($query = array())
    {
        $select = new Select(array('vt' =>$this->getTable()));
        
        $select->join(
            array('org' => 'hy_organization'),
            'vt.org_id = org.org_id',
            array('org_name') ,
            $select::JOIN_LEFT
        );
        
        $where = new Where();
        if(!empty($query['name'])) {
            $where->like('vt.vt_name', '%'.$query['name'].'%');
        }
        if(!empty($query['org_name'])) {
            $where->like('org.org_name', '%'.$query['org_name'].'%');
        }
//         if(!empty($query['start_time'])) {
//             $where->greaterThan('vt.vt_start_time', $query['start_time']);
//         }
//         if(!empty($query['end_time'])) {
//             $where->lessThan('vt.vt_end_time', $query['end_time']);
//         }
        $select->where($where);
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('vt.org_id' => $_SESSION['account']['org_id']));
        }
        
        $select->order('vt.vt_create_time DESC');
        return $this->fetchPaginate($select,$query);
    }
    public function getByVoucherId($vt_id)
    {
        $rowset = $this->select(array('vt_id' => $vt_id));
        $row = $rowset->current();
        return $row;
    }
    public function getByVoucherCode($vt_code)
    {
        $rowset = $this->select(array('vt_code' => $vt_code));
        $row = $rowset->current();
        return $row;
    }
    public function updateItem($id,$data){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $item = array(
            'vt_name'=>$data['name'],
            'vt_price'=>$data['price'],
            'vt_remark'=>$data['remark'],
            'vt_start_time'=>$data['start_time'],
            'vt_end_time'=>$data['end_time'],
            'vt_limit_price'=>$data['limit_price'],
            'org_id'=>empty($data['org_id'])?new Expression('NULL'):$data['org_id'],
            'product_id'=>empty($data['product_id'])?new Expression('NULL'):$data['product_id'],
        );
//         if(!empty($data['remark'])) {
//             $item['vt_remark']=$data['remark'];
//         }
//         if(!empty($data['org_id'])) {
//             $item['org_id']=$data['org_id'];
//         }
        if(empty($data['org_id'])) {
            $item['vt_type']= 1;
        }elseif(!empty($data['org_id']) && empty($data['product_id'])){
            $item['vt_type']= 2;
        }elseif(!empty($data['org_id']) && !empty($data['product_id'])){
            $item['vt_type']= 3;
        }
        
        return $this->update($item,array('vt_id'=>$id));
    }
    
    public function addItem($data){
    
        $item = array(
            'vt_name'=>$data['name'],
            'vt_price'=>$data['price'],
            'vt_num'=>$data['num'],
            //'vt_remark'=>$data['remark'],
            'vt_start_time'=>$data['start_time'],
            'vt_end_time'=>$data['end_time'],
            'vt_limit_price'=>$data['limit_price'],
            'vt_code'=>$data['vt_code'],
            'vt_create_time'=>new Expression('NOW()'),
    
        );
        if(!empty($data['org_id'])) {
            $item['org_id']=$data['org_id'];
        }
        if(!empty($data['remark'])) {
            $item['vt_remark']=$data['remark'];
        }
        if(!empty($data['product_id'])) {
            $item['product_id']=$data['product_id'];
        }
        if(empty($data['org_id'])) {
            $item['vt_type']= 1;
        }elseif(!empty($data['org_id']) && empty($data['product_id'])){
            $item['vt_type']= 2;
        }elseif(!empty($data['org_id']) && !empty($data['product_id'])){
            $item['vt_type']= 3;
        }
        $this->insert($item);
        $id = $this->lastInsertValue;
        return $id;
    }
    public function deleteItem($id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        return $this->delete(array('vt_id'=>$id));
    
    }
    public function getOutExcelList()
    {
        $select = new Select(array('vt' =>$this->getTable()));
         
        $select->order('vt.vt_id');
         
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }
}